package com.example.day0525

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.base.mvvm.BaseActivity
import com.example.day0525.databinding.ActivityNewsBinding
import com.example.day0525.news.NewAdapter
import com.example.day0525.news.NewViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding,NewViewModel>() {
    lateinit var newAdapter: NewAdapter
    override fun initView() {
        newAdapter= NewAdapter()
        binding.rv.layoutManager=LinearLayoutManager(this)
        binding.rv.adapter=newAdapter
        vm.news()
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                newAdapter.notifyItemRemoved(position)

            }

        })
    }

    override fun initData() {
        vm.success.observe(this){
            newAdapter.data.clear()
            newAdapter.data.addAll(it)
            newAdapter.notifyDataSetChanged()
        }
    }

}